package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mapper.SysGoodsMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysGoods;
import com.ruoyi.system.service.ISysGoodsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品发布信息Controller
 * 
 * @author ruoyi
 * @date 2025-04-21
 */
@Api("商品发布信息")
@RestController
@RequestMapping("/system/goods")
public class SysGoodsController extends BaseController
{
    @Autowired
    private ISysGoodsService sysGoodsService;
    @Autowired
    private SysGoodsMapper sysGoodsMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询商品发布信息列表
     */
    @ApiOperation("查询商品发布信息列表")
    // @PreAuthorize("@ss.hasPermi('system:goods:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysGoods sysGoods)
    {
        startPage();
        List<SysGoods> list = sysGoodsService.selectSysGoodsList(sysGoods);
        for (SysGoods item : list){
            if (!org.springframework.util.ObjectUtils.isEmpty(item.getUpdateBy())) {
                Long aLong = Long.parseLong(item.getUpdateBy());
                SysUser sysUser = sysUserMapper.selectUserById(aLong);
                item.setUpdateByName(sysUser.getUserName());
            }
        }
        return getDataTable(list);
    }

    /**
     * 查询商品发布信息列表
     */
    @ApiOperation("查询商品发布信息列表")
    // @PreAuthorize("@ss.hasPermi('system:goods:list')")
    @GetMapping("/raiList")
    public AjaxResult raiList(SysGoods sysGoods)
    {
//        startPage();
        List<SysGoods> res = new ArrayList<>();
        List<String> raiGoods = sysGoods.getRaiGoods();
        if (!ObjectUtils.isEmpty(raiGoods)) {
            for (String raiGood : raiGoods) {
                SysGoods sysGood = new SysGoods();
                sysGood.setId(raiGood);
                res.add(sysGoodsMapper.selectSysGoodsById(raiGood));
            }
        }
        return AjaxResult.success(res);
    }

    /**
     * 导出商品发布信息列表
     */
    @ApiOperation("导出商品发布信息列表")
    // @PreAuthorize("@ss.hasPermi('system:goods:export')")
    @Log(title = "商品发布信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysGoods sysGoods)
    {
        List<SysGoods> list = sysGoodsService.selectSysGoodsList(sysGoods);
        ExcelUtil<SysGoods> util = new ExcelUtil<SysGoods>(SysGoods.class);
        util.exportExcel(response, list, "商品发布信息数据");
    }

    /**
     * 获取商品发布信息详细信息
     */
    @ApiOperation("获取商品发布信息详细信息")
    // @PreAuthorize("@ss.hasPermi('system:goods:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(sysGoodsService.selectSysGoodsById(id));
    }

    /**
     * 获取商品发布信息详细信息
     */
    @ApiOperation("获取商品发布信息详细信息")
    // @PreAuthorize("@ss.hasPermi('system:goods:query')")
    @GetMapping(value = "/{id}/{storeId}")
    public AjaxResult getInfoStoreId(@PathVariable("id") String id,@PathVariable("storeId")String storeId)
    {
        return success(sysGoodsService.selectSysGoodsById(id,storeId));
    }

    /**
     * 新增商品发布信息
     */
    @ApiOperation("新增商品发布信息")
    // @PreAuthorize("@ss.hasPermi('system:goods:add')")
    @Log(title = "商品发布信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysGoods sysGoods)
    {
        if (ObjectUtils.isNotEmpty(sysGoods.getGoodsLabelList())){
            sysGoods.setGoodsLabel(sysGoods.getGoodsLabelList().toString());
        }
        if (ObjectUtils.isNotEmpty(sysGoods.getGoodsSpecList())){
            sysGoods.setGoodsSpec(JSON.toJSONString(sysGoods.getGoodsSpecList()));
        }
        return toAjax(sysGoodsService.insertSysGoods(sysGoods));
    }

    /**
     * 修改商品发布信息
     */
    @ApiOperation("修改商品发布信息")
    // @PreAuthorize("@ss.hasPermi('system:goods:edit')")
    @Log(title = "商品发布信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysGoods sysGoods)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        sysGoods.setUpdateBy(user.getUserId() + "");
        if (ObjectUtils.isNotEmpty(sysGoods.getGoodsLabelList())){
            sysGoods.setGoodsLabel(sysGoods.getGoodsLabelList().toString());
        }
        if (ObjectUtils.isNotEmpty(sysGoods.getGoodsSpecList())){
            sysGoods.setGoodsSpec(JSON.toJSONString(sysGoods.getGoodsSpecList()));
        }
        return toAjax(sysGoodsService.updateSysGoods(sysGoods));
    }

    /**
     * 删除商品发布信息
     */
    @ApiOperation("删除商品发布信息")
    // @PreAuthorize("@ss.hasPermi('system:goods:remove')")
    @Log(title = "商品发布信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(sysGoodsService.deleteSysGoodsByIds(ids));
    }
}
